package com.anlogic.sdk.bootimage.internal.ui.common;

import com.anlogic.sdk.bootimage.internal.ui.composite.AbsBootgenComposite;
import com.anlogic.sdk.utils.StringUtils;
import com.anlogic.sdk.utils.UIUtils;
import java.io.File;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class BootgenTextField implements IExtBootgenField<Text, String> {
	private AbsBootgenComposite bootgenComposite;

	private IModifyListener<String> modifyListener;

	private IFileBrowseInputInitListener browseInputInitListener;

	private Label label;

	private Text text;

	private Button browseButton;

	public BootgenTextField(AbsBootgenComposite bootgenComposite) {
		this.bootgenComposite = bootgenComposite;
	}

	public void createField(Composite container, String labelText) {
		if (StringUtils.isNotEmpty(labelText)) {
			this.label = new Label(container, 0);
			this.label.setText(labelText);
			GridDataFactory.swtDefaults().applyTo((Control) this.label);
		}
		this.text = new Text(container, 2048);
		this.text.setData(this.label);
		this.text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (!BootgenTextField.this.bootgenComposite.isSkipNotifactions()
						&& BootgenTextField.this.modifyListener != null)
					BootgenTextField.this.modifyListener.valueChanged(BootgenTextField.this.getSelection());
			}
		});
	}

	public void createBrowseFolderField(Composite container, String fieldName, String prefKey) {
		String labelText = convertToLabelText(fieldName);
		IFileBrowseInput browseInput = FileBrowseInput.createOpenDialogInput(labelText, prefKey, null);
		createBrowseFolderFromInput(container, labelText, browseInput);
	}

	private void createBrowseFolderFromInput(Composite container, String labelText,
			final IFileBrowseInput browseInput) {
		createField(container, labelText);
		this.browseButton = new Button(container, 0);
		this.browseButton.setText("Browse...");
		GridDataFactory.swtDefaults().applyTo((Control) this.browseButton);
		this.browseButton.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (BootgenTextField.this.browseInputInitListener != null) {
					BootgenTextField.this.browseInputInitListener.initialize(browseInput);
				} else {
					String filePath = BootgenTextField.this.text.getText();
					if (!StringUtils.isEmpty(filePath)) {
						File file = new File(filePath);
						File parentFile = file.getParentFile();
						if (parentFile != null)
							browseInput.setFilterPath(parentFile.getAbsolutePath());
						browseInput.setFileName(file.getName());
					}
				}
				String path = UIUtils.openNativeFolderDialog(browseInput.getTitle(), browseInput.getPrefKey(),
						browseInput.getFilterPath());
				if (StringUtils.isNotEmpty(path))
					BootgenTextField.this.text.setText(path);
			}
		});
		if (this.label != null) {
			this.label.setData(this.browseButton);
		} else {
			this.text.setData(this.browseButton);
		}
	}

	public void createOpenBrowseField(Composite container, String fieldName, String prefKey, String[] extensions) {
		createOpenBrowseField(container, convertToLabelText(fieldName), fieldName, prefKey, extensions);
	}

	public void createOpenBrowseField(Composite container, String labelText, String title, String prefKey,
			String[] extensions) {
		IFileBrowseInput browseInput = FileBrowseInput.createOpenDialogInput(title, prefKey, extensions);
		createBrowseFieldFromInput(container, labelText, browseInput);
	}

	public void createSaveBrowseField(Composite container, String fieldName, String prefKey, String[] extensions) {
		createSaveBrowseField(container, convertToLabelText(fieldName), fieldName, prefKey, extensions);
	}

	public void createSaveBrowseField(Composite container, String labelText, String title, String prefKey,
			String[] extensions) {
		IFileBrowseInput browseInput = FileBrowseInput.createSaveDialogInput(title, prefKey, extensions);
		createBrowseFieldFromInput(container, labelText, browseInput);
	}

	private String convertToLabelText(String fieldName) {
		return String.valueOf(fieldName) + ": ";
	}

	private void createBrowseFieldFromInput(Composite container, String labelText, final IFileBrowseInput browseInput) {
		createField(container, labelText);
		this.browseButton = new Button(container, 0);
		this.browseButton.setText("Browse...");
		GridDataFactory.swtDefaults().applyTo((Control) this.browseButton);
		this.browseButton.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (BootgenTextField.this.browseInputInitListener != null) {
					BootgenTextField.this.browseInputInitListener.initialize(browseInput);
				} else {
					String filePath = BootgenTextField.this.text.getText();
					if (!StringUtils.isEmpty(filePath)) {
						File file = new File(filePath);
						File parentFile = file.getParentFile();
						if (parentFile != null)
							browseInput.setFilterPath(parentFile.getAbsolutePath());
						browseInput.setFileName(file.getName());
					}
				}
				String path = UIUtils.openNativeFileDialog(browseInput.getType(), browseInput.getTitle(),
						browseInput.getPrefKey(), browseInput.getExtensions(), browseInput.getFileName(),
						browseInput.getFilterPath());
				if (StringUtils.isNotEmpty(path))
					BootgenTextField.this.text.setText(path);
			}
		});
		if (this.label != null) {
			this.label.setData(this.browseButton);
		} else {
			this.text.setData(this.browseButton);
		}
	}

	public String getSelection() {
		return this.text.getText();
	}

	public Label getLabel() {
		return this.label;
	}

	public void setModifyListener(IModifyListener<String> modifyListener) {
		this.modifyListener = modifyListener;
	}

	public void setBrowseInputInitListener(IFileBrowseInputInitListener browseInputInitListener) {
		this.browseInputInitListener = browseInputInitListener;
	}

	public void setSelection(String selection) {
		if (selection == null)
			selection = "";
		this.text.setText(selection);
	}

	public void setEnabled(boolean enabled) {
		getControl().setEnabled(enabled);
		if (this.label != null)
			this.label.setEnabled(enabled);
		if (this.browseButton != null)
			this.browseButton.setEnabled(enabled);
	}

	public Text getControl() {
		return this.text;
	}

	public void setDefaultGridData(int hSpan) {
		GridDataFactory.fillDefaults().grab(true, false).span(hSpan, 1).applyTo((Control) getControl());
	}
}
